Lesson Log on Class:


REMEMBER THIS MY FELLOW CLASSMATES

DO NOT EVER WRITE ON WIKI

WITHOUT BACKUP STORAGE

ALRIGHT?

y'all bear this in mind OK?



Now let’s get started 😄

First of all let’s review three evolutionary thresholds of programming language:


Low-level programming:machine code

procedural paradigm

object oriented paradigm

(The programming mode we previously used in python was called procedural programming, )

However, in object oriented paradigm:


Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of objects that interact with one another.[1][2] There is significant diversity of OOP languages, but the most popular ones are class-based, meaning that objects are instances of classes, which typically also determine their type.(adapted from Wikipedia)

Let’s understand this by specific program:


classPoint:
""" Point class represents and manipulates x,y coords. """

definit(self):
""" Create a new point at the origin """
self.x =0
self.y = 0

👆 This is how we define a class
There are few points:

You may find it pretty confusing that what does “self” mean ?

It will be representing the object which user provides,
as in

p = Point()

here for each self represents p

Then if we want to know x, y coordinates of the point what do we do?

p.x p.y

is the correct way to get them

x,y here is what we call attributes, it is one of the special characteristics of the class you defined

The other special characteristic is the method, let’s see an example


in the class point:

defdistance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x 2) + (self.y 2)) 0.5

This is the function in which you can compute the distance from the point to the origin

But can we add parameter to the function in class?

the following function illustrate this
defhalfway(self, target):
""" Return the halfway point between myself and the target """
mx = (self.x + target.x)/2
my = (self.y + target.y)/2
return Point(mx, my)

This is the function which we calculate the mid-point and we return a point value which includes two coordinates value after computation. It is worth noting that if we want our output to be Point(mx, my), we must alter our original definition in the class to be

classPoint:
""" Point class represents and manipulates x,y coords. """

def**init(self,x,y):
""" Create a new point at the origin """
self.x =x
self.y =y

OK we know how to define a function in the class

How do we call it then

For these function which returns a point value things may get a little tricky

you have to write like

q = Point(5, 12)
p = Point(3, 4)
r = p.halfway(q)


should give you this result
(4.0, 8.0)


Homework:

calculate 3D point using class point